/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package boxoffice;

import java.awt.*;
import javax.swing.*;
import boxoffice.DateChooser;
import boxoffice.Event;
import boxoffice.EventData;
import boxoffice.ImageScrollFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * My Java application using Swing components to build the GUI.
 * Now uses the EventData Java file for event data!
 * @author Alex
 */
public class BoxOffice extends JFrame implements ActionListener
{
    public static final Dimension FRAMESIZE = new Dimension(800, 400);
    
    // Lists
    private JComboBox events;
    private JComboBox venues;
    private JDialog datesparent;
    private JComboBox results;
    
    // Data
    Event[] listOfEvents = EventData.getEvents();
        
    // Buttons
    private JButton dateMinimum;
    private JButton dateMaximum;
    private JButton search;
    private JButton moreDetails;
    private DateChooser dateFirst;
    private DateChooser dateSecond;
    
    // Windows
    private TicketWindow tickWin;
    
    /**
     * BoxOffice - Your typical constructor, 
     * nothing too special. Just lets everything get off the ground and launch.
     * @param args the command line arguments
     */
    
    public BoxOffice() {
        super("Box Office");
        
        drawWindow();
        
        layoutStuff();
        
        dataRun();
        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    
    /**
     * drawWindow() - This is where I initialize all the buttons and
     * combo boxes for placement in the window.
     */
    public void drawWindow() {
        events = new JComboBox();
        venues = new JComboBox();
        datesparent = new JDialog();
        dateFirst = new DateChooser(datesparent);
        dateSecond = new DateChooser(datesparent);
        
        dateMinimum = new JButton("Choose First Date");
        dateMaximum = new JButton("Choose Second Date");
        search = new JButton("Search");
        moreDetails = new JButton("Show More Details");
        
        results = new JComboBox();
        
        tickWin = new TicketWindow();
    }
    
    /**
     * drawNorth() - JPanel constructor function for building the
     * panel that's going to be placed at the Northern part of my JFrame.
     * @return northBorder JPanel for placement in the JFrame parent.
     */
    private JPanel drawNorth() {
        
        JPanel northBorder = new JPanel(new GridLayout(4, 0));
        
        northBorder.add(events, BorderLayout.NORTH);
        northBorder.add(venues, BorderLayout.NORTH);
        northBorder.add(dateMinimum, BorderLayout.NORTH);
        northBorder.add(dateMaximum, BorderLayout.NORTH);
        
        return northBorder;
    }
    
    /**
     * drawWest() - Serves exactly the same purpose as drawNorth().
     * 
     * @return westBorder JPanel for placement in the JFrame parent.
     */
    private JPanel drawWest() {
        JPanel westBorder = new JPanel(new GridLayout(3,0));
        
        westBorder.add(search, BorderLayout.WEST);
        westBorder.add(results, BorderLayout.WEST);
        westBorder.add(moreDetails, BorderLayout.WEST);
        
        return westBorder;
    }
    
    /**
     * layoutStuff() - This is where we place everything in the JFrame content
     * pane.
     */
    public void layoutStuff() {        
        Container content = this.getContentPane();
        
        content.setLayout(new BorderLayout());
        
        JPanel northBorder = drawNorth();
        JPanel westBorder = drawWest();
        
        content.add(northBorder, BorderLayout.NORTH);
        
        content.add(westBorder, BorderLayout.WEST);
        
        search.addActionListener(this);
        dateMinimum.addActionListener(this);
        dateMaximum.addActionListener(this);
        moreDetails.addActionListener(this);
        
        this.setSize(FRAMESIZE);
        this.setPreferredSize(FRAMESIZE);
        this.setVisible(true);
    }

    /**
     * dataRun() - Retrieves the necessary data from EventData.java, and
     * sets up all my combo box text entries.
     */
    public void dataRun() {
        events.addItem(new String("All"));
        events.addItem(new String("Music"));
        events.addItem(new String("Theater"));
        events.addItem(new String("Gaming"));
        
        venues.addItem(new String("All"));
        venues.addItem(new String("MacPherson"));
        venues.addItem(new String("Royal"));
        venues.addItem(new String("Belfry"));
        
        GregorianCalendar startCalendar = new GregorianCalendar();
        startCalendar.set(Calendar.YEAR, 1900);
        startCalendar.set(Calendar.MONTH, 1);
        startCalendar.set(Calendar.DATE, 1);
        
        GregorianCalendar endCalendar = new GregorianCalendar();
        endCalendar.set (Calendar.YEAR, 2099);
        endCalendar.set (Calendar.MONTH, 12);
        endCalendar.set (Calendar.DATE, 30);
        
        dateFirst.selectQuiet(startCalendar.getTime());
        dateSecond.selectQuiet(endCalendar.getTime());
    }
    
    /**
     * main() - Kicks off the entire process by creating a new 
     * BoxOffice object.
     * @param args They do nothing for now.
     */
    public static void main(String[] args) {
        new BoxOffice();
    }
    
    /**
     * actionPerformed() - The great overseer of my program. This watches
     * for any actions that are of importance to the program, and will fire
     * the necessary operations to attend to these events.
     * 
     * @param e The event that occurred.
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == search) {
            results.removeAllItems();
            
            for(Event ev : listOfEvents) {
                if (events.getSelectedItem().toString().equals(ev.getType()) | 
                        events.getSelectedItem().toString().equals("All")) {
                    if (venues.getSelectedItem().toString().equals(ev.getVenue
                            ()) | venues.getSelectedItem().
                            toString().equals("All")) {
                        if ((dateFirst.getDate().before(ev.getDate()) & 
                                dateSecond.getDate().after(ev.getDate())) | 
                                (dateFirst.getDate() == null & dateSecond.
                                getDate() == null)) {
                            results.addItem(new String("Event: " + ev.getType() 
                                    + 
                                    " Name: " + ev.getName() + 
                                    " Description: " + ev.getDesc()));
                        }
                    }
                }
            }
        }
        
        if (e.getSource() == dateMinimum) {
            dateFirst.setVisible(true);
        }
        
        if (e.getSource() == dateMaximum) {
            dateSecond.setVisible(true);
        }
        
        if (e.getSource() == moreDetails) {
            for(Event ev : listOfEvents) {
                if (results.getSelectedItem().equals("Event: " + ev.getType() + 
                        " Name: " + ev.getName() + " Description: " 
                        + ev.getDesc())) {
                    tickWin.sendData(ev);
                    tickWin.setVisible(true);
                }
            }
        }
    }
}
